package cn.fl.riskctrl.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.product.service.ISplInfoService;
import cn.fl.system.service.ISysDictionaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.riskctrl.service.IRskCtrlCfgService;
import cn.fl.riskctrl.transform.RskCtrlCfgDVOTransform;
import cn.fl.riskctrl.transform.RskCtrlCfgMVOTransform;
import cn.fl.riskctrl.vo.RskCtrlCfgDVO;
import cn.fl.riskctrl.vo.RskCtrlCfgMVO;
import fl.riskctrl.facade.riskctrl.api.IRiskCtrlAddApiService;
import fl.riskctrl.facade.riskctrl.api.IRiskCtrlModifyApiService;
import fl.riskctrl.facade.riskctrl.api.IRiskCtrlRemoveApiService;
import fl.riskctrl.facade.riskctrl.api.IRiskCtrlSearchApiService;
import fl.riskctrl.facade.riskctrl.condition.RskCtrlCfgDQC;
import fl.riskctrl.facade.riskctrl.dto.RskCtrlCfgDDTO;
import fl.riskctrl.facade.riskctrl.dto.RskCtrlCfgMDTO;

/**
 * Created by Administrator on 2017/6/5.
 */
@Service
public class RskCtrlCfgServiceImpl implements IRskCtrlCfgService {

    private static final Logger logger = LoggerFactory.getLogger(RskCtrlCfgServiceImpl.class);

    @Autowired
    private IRiskCtrlSearchApiService riskCtrlSearchApiService;

    @Autowired
    private IRiskCtrlAddApiService riskCtrlAddApiService;

    @Autowired
    private IRiskCtrlRemoveApiService riskCtrlRemoveApiService;

    @Autowired
    private IRiskCtrlModifyApiService riskCtrlModifyApiService;

    @Autowired
    private ISplInfoService prdSplInfoService;

    @Autowired
    private ISysDictionaryService sysDictionaryService;

    @Override
    public Pager searchListPage(Pager page) {
        if (null == page) {
            page = new Pager();
        }
        Pager pager = this.riskCtrlSearchApiService.searchRiskCtrlCfgMListPage(page);
        List<RskCtrlCfgMDTO> resultList = (List<RskCtrlCfgMDTO>) pager.getResultList();

        // 事业部
        // List<KeyValueVO> searchBuOrgList = this.prdSplInfoService.searchBuOrgCdList();

        // 数据字典（系统名+是否启用）
        List<String> codeGrpCdList = new ArrayList<>();
        codeGrpCdList.add(DictionaryConstant.CODE_SYS_DS_CD);
        codeGrpCdList.add(DictionaryConstant.CODE_GRP_IS_VALID_CD);
        // 征信规则业务编码
        codeGrpCdList.add(PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.DictionaryConstant.RC_CFG_BUS_CD));
        Map<String, List<KeyValueVO>> dicMap = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
        // 系统信息
        List<KeyValueVO> sysList = dicMap.get(DictionaryConstant.CODE_SYS_DS_CD);
        Map<String, String> sysMap = null;
        if (CollectionUtils.isNotEmpty(sysList)) {
            sysMap = new HashMap<>(sysList.size());
            for (KeyValueVO dto : sysList) {
                sysMap.put(dto.getKey(), dto.getValue());
            }
        }
        //
        List<KeyValueVO> statusList = dicMap.get(DictionaryConstant.CODE_GRP_IS_VALID_CD);
        Map<String, String> statusMap = null;
        if (CollectionUtils.isNotEmpty(statusList)) {
            statusMap = new HashMap<>(statusList.size());
            for (KeyValueVO dto : statusList) {
                statusMap.put(dto.getKey(), dto.getValue());
            }
        }
        // 征信规则业务编码
        List<KeyValueVO> searchBuOrgList = dicMap.get(PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.DictionaryConstant.RC_CFG_BUS_CD));
        Map<String, String> orgMap = null;
        if (CollectionUtils.isNotEmpty(searchBuOrgList)) {
            orgMap = new HashMap<>(searchBuOrgList.size());
            for (KeyValueVO dto : searchBuOrgList) {
                orgMap.put(dto.getKey(), dto.getValue());
            }
        }

        List<RskCtrlCfgMVO> rskCtrlCfgMVOList = RskCtrlCfgMVOTransform.toVOList(resultList);
        for (RskCtrlCfgMVO rskCtrlCfgMVO : rskCtrlCfgMVOList) {
            if (orgMap != null && orgMap.containsKey(rskCtrlCfgMVO.getOrgCd())) {
                rskCtrlCfgMVO.setOrgCdNm(orgMap.get(rskCtrlCfgMVO.getOrgCd()));
            }
            if (sysMap != null && sysMap.containsKey(rskCtrlCfgMVO.getSysCd())) {
                rskCtrlCfgMVO.setSysCdNm(sysMap.get(rskCtrlCfgMVO.getSysCd()));
            }
            if (statusMap != null && statusMap.containsKey(String.valueOf(rskCtrlCfgMVO.getIsValid()))) {
                rskCtrlCfgMVO.setIsOpen(statusMap.get(String.valueOf(rskCtrlCfgMVO.getIsValid())));
            }
        }
        pager.setResultList(rskCtrlCfgMVOList);
        return pager;
    }

    @Override
    public List<RskCtrlCfgDVO> searchListSon(RskCtrlCfgDQC qc) {
        if (null == qc) qc = new RskCtrlCfgDQC();
        List<RskCtrlCfgDDTO> searchRiskCtrlCfgDList = this.riskCtrlSearchApiService.searchRiskCtrlCfgDList(qc);
        //数据字典（是否启用+是否校验）
        List<String> codeGrpCdList = new ArrayList<>();
        codeGrpCdList.add(DictionaryConstant.CODE_GRP_IS_VALID_CD);
        codeGrpCdList.add(DictionaryConstant.CODE_GRP_VALID_STATUS_CD);
        Map<String, List<KeyValueVO>> dicMap = this.sysDictionaryService.getMultiKvList(CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
        List<KeyValueVO> statusList = dicMap.get(DictionaryConstant.CODE_GRP_IS_VALID_CD);
        List<KeyValueVO> checkList = dicMap.get(DictionaryConstant.CODE_GRP_VALID_STATUS_CD);
        Map<String, String> statusMap = null;
        if (CollectionUtils.isNotEmpty(statusList)) {
            statusMap = new HashMap<>(statusList.size());
            for (KeyValueVO dto : statusList) {
                statusMap.put(dto.getKey(), dto.getValue());
            }
        }
        Map<String, String> checkMap = null;
        if (CollectionUtils.isNotEmpty(checkList)) {
            checkMap = new HashMap<>(checkList.size());
            for (KeyValueVO dto : checkList) {
                checkMap.put(dto.getKey(), dto.getValue());
            }
        }
        List<RskCtrlCfgDVO> rskCtrlCfgDVOList = RskCtrlCfgDVOTransform.toVOList(searchRiskCtrlCfgDList);
        for (RskCtrlCfgDVO rskCtrlCfgDVO : rskCtrlCfgDVOList) {
            if (statusMap != null && statusMap.containsKey(String.valueOf(rskCtrlCfgDVO.getIsValid()))) {
                rskCtrlCfgDVO.setIsOpen(statusMap.get(String.valueOf(rskCtrlCfgDVO.getIsValid())));
            }
            if (checkMap != null && checkMap.containsKey(String.valueOf(rskCtrlCfgDVO.getIsCheck()))) {
                rskCtrlCfgDVO.setIsValidate(checkMap.get(String.valueOf(rskCtrlCfgDVO.getIsCheck())));
            }
        }
        return rskCtrlCfgDVOList;
    }

    @Override
    public Long add(RskCtrlCfgMVO entity) {
        if (null == entity) {
            BaseLogUtils.info(logger, "add", "新增风控配置主表信息失败,风控配置主表对象参数为空.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlAddApiService.addRiskCtrlCfgM(RskCtrlCfgMVOTransform.toDTO(entity));
    }

    @Override
    public Long add(RskCtrlCfgDVO entity) {
        if (null == entity) {
            BaseLogUtils.info(logger, "add", "新增风控配置子表信息失败,风控配置子表对象参数为空.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlAddApiService.addRiskCtrlCfgD(RskCtrlCfgDVOTransform.toDTO(entity));
    }

    @Override
    public Boolean modify(RskCtrlCfgMVO entity) {
        if (null == entity) {
            BaseLogUtils.info(logger, "modify", "修改风控配置主表信息失败,风控配置主表对象参数为空.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlModifyApiService.modifyRiskCtrlCfgM(RskCtrlCfgMVOTransform.toDTO(entity));
    }

    @Override
    public Boolean modify(RskCtrlCfgDVO entity) {
        if (null == entity) {
            BaseLogUtils.info(logger, "modify", "修改风控配置子表信息失败,风控配置子表对象参数为空.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlModifyApiService.modifyRiskCtrlCfgD(RskCtrlCfgDVOTransform.toDTO(entity));
    }

    @Override
    public Boolean modifyRMIsvalid(Long id, int isValid) {
        if (null == id) {
            BaseLogUtils.info(logger, "modifyRMIsvalid", "修改风控配置主表状态失败,风控配置子表对象参数为空.DATA:" + id, CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlModifyApiService.modifyRMCfgIsValid(id, isValid);
    }

    @Override
    public Boolean modifyRDIsvalid(Long id, int isValid) {
        if (null == id) {
            BaseLogUtils.info(logger, "modifyRDIsvalid", "修改风控配置子表状态失败,风控配置子表对象参数为空.DATA:" + id, CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlModifyApiService.modifyRDCfgIsValid(id, isValid);
    }

    @Override
    public Boolean modifyRDIsCheck(Long id, int isCheck) {
        if (null == id) {
            BaseLogUtils.info(logger, "modifyRDIsCheck", "修改风控配置子表验证状态失败,风控配置子表验证状态对象参数为空.DATA:" + id, CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlModifyApiService.modifyRDCfgIsCheck(id, isCheck);
    }

    @Override
    public Boolean removeById(Long id) {
        if (id == null) {
            BaseLogUtils.info(logger, "removeById", "删除风控配置主表信息失败,风控配置主表对象参数为空.DATA:" + id, CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlRemoveApiService.removeRiskCtrlCfgMById(id);
    }

    @Override
    public Boolean removeSONById(Long id) {
        if (id == null) {
            BaseLogUtils.info(logger, "removeSONById", "删除风控配置子表信息失败,风控配置子表对象参数为空.DATA:" + id, CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.riskCtrlRemoveApiService.removeRiskCtrlCfgDById(id);
    }

    @Override
    public RskCtrlCfgMVO selectById(Long id) {
        if (id == null) {
            BaseLogUtils.info(logger, "selectById", "修改风控配置主表信息时查询一条数据失败,风控配置主表对象参数为空.DATA:" + JSONObject.toJSONString(id), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        RskCtrlCfgMDTO dto = this.riskCtrlSearchApiService.selectRiskCtrlCfgMById(id);
        return RskCtrlCfgMVOTransform.toVO(dto);
    }

    @Override
    public RskCtrlCfgDVO selectSONById(Long id) {
        if (id == null) {
            BaseLogUtils.info(logger, "selectSONById", "修改风控配置子表信息时查询一条数据失败,风控配置子表对象参数为空.DATA:" + JSONObject.toJSONString(id), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        RskCtrlCfgDDTO dto = this.riskCtrlSearchApiService.selectRiskCtrlCfgDById(id);
        return RskCtrlCfgDVOTransform.toVO(dto);
    }
}
